<template>
  <div class="page">
    <transition name="el-fade-in">
      <div class="mask" v-show="show_edit" @click="show_edit = false">
        <div @click.stop>
          <div class="left">
            <el-upload
              v-loading="image_loading"
              element-loading-background="rgba(0, 0, 0, 0.8)"
              class="avatar-uploader"
              :action="$store.state.url + 'upload/image'"
              :show-file-list="false"
              :on-success="uploadImage"
              :before-upload="showLoading"
            >
              <img v-if="user_head" :src="user_head" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <p>点击图片修改头像</p>
          </div>
          <div class="right">
            <span>姓名</span>
            <el-input v-model="user_name"></el-input>
            <span>邮箱</span>
            <el-input v-model="user_email"></el-input>
            <div class="btn">
              <el-button type="primary" @click="save">保存</el-button>
            </div>
          </div>
        </div>
      </div>
    </transition>
    <transition name="el-fade-in">
      <div class="mask" v-show="show_apply" @click="show_apply = false">
        <div @click.stop>
          <div class="right">
            <el-input v-model="apply_email" placeholder="请填写证书接收邮箱"></el-input>
            <div class="mine-button" @click="apply">申请</div>
          </div>
        </div>
      </div>
    </transition>
    <div class="info">
      <div class="head">
        <img :src="user_head" alt />
        <div>
          <span>
            Hi，欢迎你 {{ user_name }}
            <i class="el-icon-edit" @click="show_edit = true"></i>
          </span>
          <div>
            <div @click="recharge" class="mine-button">{{ $store.state.is_vip ? "续费会员" : "购买会员" }}</div>
            <span
              v-if="$store.state.is_buy_lesson || $store.state.vip_start || $store.state.vip_end"
            >已购买{{$store.state.is_buy_lesson}}门永久观看单课程；年费会员开始时间{{$store.state.vip_start}} 到期时间{{$store.state.vip_end}}</span>
          </div>
        </div>
      </div>
      <div>
        <!-- 左侧菜单 -->
        <div class="left">
          <div
            v-for="(item, index) in nav_list"
            :key="index"
            @click="$store.state.mine_index = index"
            :class="$store.state.mine_index == index ? 'active' : ''"
          >
            <span>{{ item }}</span>
            <div></div>
          </div>
        </div>
        <!-- 继续学习 -->
        <div class="right lesson" v-show="$store.state.mine_index == 0">
          <div
            v-for="(item, index) in history_list"
            :key="index"
            @click="
              $router.push({
                path: '/Teacher/detail',
                query: {
                  id: item.id
                }
              })
            "
          >
            <img :src="item.image" alt />
            <div>{{ item.name }}</div>
          </div>
        </div>
        <!-- 我的课程 -->
        <div class="right lesson" v-show="$store.state.mine_index == 1">
          <div
            v-for="(item, index) in list"
            :key="index"
            @click="
              $router.push({
                path: '/Teacher/index',
                query: {
                  id: item.id
                }
              })
            "
          >
            <img :src="item.image" alt />
            <div>{{ item.name }}</div>
          </div>
        </div>
        <!-- 学习档案 -->
        <div class="right learn" v-show="$store.state.mine_index == 2">
          <el-checkbox-group v-model="check_list" text-color="gold" fill="#facd8a">
            <div v-for="(item, index) in learn_list" :key="index" class="checkbox-group">
              <el-checkbox :label="item.id">
                <div class="checkbox-item">
                  <img :src="item.image" alt />
                  <span>你已经完成了{{ item.name }}的共{{ item.number }}节课</span>
                </div>
              </el-checkbox>
            </div>
          </el-checkbox-group>
          <div class="mine-button" v-if="learn_list.length" @click="showApply">申请证书</div>
          <div v-else>
            <div>您暂无学习档案</div>
            <el-button type="primary" @click="$router.push('/')">继续学习</el-button>
          </div>
        </div>
        <!-- 消息中心 -->
        <div class="right message" v-show="$store.state.mine_index == 3">
          <el-collapse v-model="activeNames" class="list" v-if="message_list.length">
            <el-collapse-item name="1" v-for="(item, index) in message_list" :key="index">
              <template slot="title">
                <div class="title">
                  <div>{{ item.title }}</div>
                  <span @click.stop="deleteMSG(item.id)">删除</span>
                </div>
              </template>
              <div class="content">{{ item.content }}</div>
            </el-collapse-item>
          </el-collapse>
          <div v-else>您目前还没有任何消息~</div>
        </div>
        <!-- 修改密码 -->
        <div class="right set" v-show="$store.state.mine_index == 4">
          <el-input v-model="form.account" maxlength="11" placeholder="请输入手机号码"></el-input>
          <el-input v-model="form.password" show-password placeholder="请输入新密码"></el-input>
          <el-input v-model="form.confirm_password" show-password placeholder="请再次确认密码"></el-input>
          <el-input v-model="form.code" maxlength="6" placeholder="请输入验证码">
            <el-button slot="append" @click="sendCode(2)">发送</el-button>
          </el-input>
          <div class="mine-button" @click="changePwd">提交</div>
        </div>
        <!-- 充值中心 -->
        <div class="right recharge" v-show="$store.state.mine_index == 5">
          <div>
            <div>
              <div>订单号</div>
              <div>产品</div>
              <div>付款时间</div>
              <div>开始时间</div>
              <div>结束时间</div>
              <div>付款价格</div>
            </div>
            <div v-for="(item,index) in recharge_list" :key="index">
              <div>{{item.id}}</div>
              <div>{{item.name}}</div>
              <div>{{item.updated_at}}</div>
              <div>{{item.start}}</div>
              <div>{{item.end}}</div>
              <div>{{item.price}}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      headers: {
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      show_edit: false,
      image_loading: false,
      list: [],
      history_list: [],
      user_head: localStorage.getItem("user_head"),
      user_name: localStorage.getItem("user_name"),
      user_email: localStorage.getItem("user_email"),
      year_price: 0,
      nav_list: [
        "继续学习",
        "我的课程",
        "学习档案",
        "消息中心",
        "修改密码",
        "充值中心"
      ],
      learn_list: [],
      message_list: [],
      activeNames: [],
      form: {
        account: "",
        code: "",
        password: "",
        confirm_password: ""
      },
      check_list: [],
      show_apply: false,
      apply_email: "",
      recharge_list: []
    };
  },
  created() {
    let _this = this;
    if (_this.$store.state.is_login) {
      _this.$axios.get("site/index2").then(res => {
        _this.history_list = res.history.list;
        _this.list = res.myLesson.list;
      });
      _this.$axios.get("site/vip").then(res => {
        _this.year_price = res.price;
      });
      _this.$axios.get("group/learn").then(res => {
        _this.learn_list = res.list;
      });
      _this.$axios.get("message").then(res => {
        _this.message_list = res.list;
      });
      _this.$axios.get("user/payment-record").then(res => {
        _this.recharge_list = res.list;
      });
    }
  },
  methods: {
    showApply() {
      let _this = this;
      if (!_this.check_list.length) {
        _this.$notify({
          title: "错误",
          message: "请选择需要申请的课程",
          type: "error"
        });
        return;
      } else {
        _this.show_apply = true;
      }
    },
    apply() {
      let _this = this;
      if (!_this.apply_email) {
        _this.$notify({
          title: "错误",
          message: "请填写证书接收邮箱",
          type: "error"
        });
        return;
      }
      _this.show_apply = false;
      let lesson_group = "";
      _this.check_list.forEach(item => {
        lesson_group += item + "|";
      });
      _this.$axios
        .post("user/apply", {
          lesson_group: lesson_group,
          email: _this.apply_email
        })
        .then(res => {
          if (res) {
            _this.$notify({
              title: "成功",
              message:
                "证书申请提交成功，我们将在3个工作日内，将证书以邮件形式发送给您，请注意查收，如有疑问请联系客服",
              type: "success"
            });
          }
        });
    },
    recharge() {
      this.$store.state.show_recharge = true;
      this.$store.state.order_type = "vip";
    },
    changePwd() {
      let _this = this;
      _this.$axios
        .post("forget-password", {
          account: _this.form.account,
          password: _this.form.password,
          passwordAgain: _this.form.confirm_password,
          code: _this.form.code
        })
        .then(res => {
          if (res) {
            _this.$notify({
              title: "成功",
              message: "密码修改成功",
              type: "success"
            });
          }
        });
    },
    sendCode(type) {
      let _this = this;
      _this.$axios
        .post("send", {
          type: type,
          mobile: _this.form.account
        })
        .then(res => {
          if (res) {
            _this.$notify({
              title: "成功",
              message: "短信验证码发送成功",
              type: "success"
            });
          }
        });
    },
    deleteMSG(id) {
      let _this = this;
      _this.$axios
        .delete("message/delete", {
          params: {
            id: id
          }
        })
        .then(res => {
          _this.$axios.get("message").then(res => {
            _this.message_list = res.list;
          });
        });
    },
    save() {
      let _this = this;
      _this.$axios
        .post("user", {
          face: _this.user_head,
          username: _this.user_name,
          email: _this.user_email
        })
        .then(res => {
          localStorage.setItem("user_head", _this.user_head);
          localStorage.setItem("user_name", _this.user_name);
          localStorage.setItem("user_email", _this.user_email);
          _this.show_edit = false;
          _this.$router.go(0);
        });
    },
    showLoading() {
      this.image_loading = true;
    },
    uploadImage(res, file) {
      this.user_head = res.data.imageUrl + res.data.path;
      this.image_loading = false;
    }
  }
};
</script>

<style lang="less" scoped>
.mask {
  width: 100%;
  height: 100vh;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  left: 0;
  top: 0;
  z-index: 2002;
  > div {
    height: 4.5rem;
    background: black;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: row;
    justify-content: center;
    box-shadow: 0 0 0.3rem 0 black;
    @media only screen and (max-width: 500px),
      only screen and (max-device-width: 500px) {
      width: calc(100% - 0.6rem);
      height: auto;
    }
    > div {
      height: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      box-sizing: border-box;
      color: @gold-color;
      @media only screen and (max-width: 500px),
        only screen and (max-device-width: 500px) {
        padding: 0.3rem 0;
      }
      > span {
        width: 50%;
        @media only screen and (max-width: 500px),
          only screen and (max-device-width: 500px) {
          width: calc(100% - 0.6rem);
        }
      }
      .el-input {
        width: 50%;
        margin-bottom: 0.3rem;
        @media only screen and (max-width: 500px),
          only screen and (max-device-width: 500px) {
          width: calc(100% - 0.6rem);
        }
      }
    }
    .left {
      width: 3.2rem;
      background: black;
    }
    .right {
      width: 4.8rem;
      background: white;
    }
  }
}
.edit {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  > div {
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    > img {
      width: 1.5rem;
      height: 1.5rem;
      margin-right: 0.3rem;
      margin-bottom: 0.3rem;
    }
  }
  .btn {
    justify-content: center;
  }
  .el-input {
    margin-bottom: 0.3rem;
  }
}
.info {
  width: @width;
  background: #191919;
  margin-top: 2rem;
  @media only screen and (max-width: 500px),
    only screen and (max-device-width: 500px) {
    width: calc(100% - 0.6rem);
  }
  > div {
    width: 100%;
    display: flex;
    flex-direction: row;
    position: relative;
    bottom: 0.3rem;
    .left {
      width: 2.3rem;
      display: flex;
      flex-direction: column;
      align-items: flex-end;
      > div {
        display: flex;
        flex-direction: row;
        align-items: center;
        cursor: pointer;
        margin-top: 0.1rem;
        > span {
          width: 1.5rem;
          height: 0.5rem;
          display: flex;
          align-items: center;
          justify-content: center;
          border-top-left-radius: 0.1rem;
          border-bottom-left-radius: 0.1rem;
          transition: 300ms;
        }
        > div {
          border: 0.25rem solid transparent;
          transition: 300ms;
        }
      }
      > div:hover {
        > span {
          background: @gold-color;
          color: @dark-color;
        }
        > div {
          border-left: 0.25rem solid @gold-color;
        }
      }
      .active {
        > span {
          background: @gold-color;
          color: @dark-color;
        }
        > div {
          border-left: 0.25rem solid @gold-color;
        }
      }
    }
    .right {
      width: calc(100% - 2.6rem);
      background: @dark-color;
      box-sizing: border-box;
      padding: 0.3rem;
      display: flex;
    }
    .learn {
      flex-direction: column;
      .checkbox-group {
        width: 100%;
        display: flex;
        flex-direction: row;
        align-items: center;
        margin-bottom: 0.1rem;
        font-size: @normal-size;
        img {
          width: 0.7rem;
          height: 0.7rem;
          margin-right: 0.2rem;
          object-fit: cover;
        }
        .el-checkbox {
          width: 100%;
          display: flex;
          flex-direction: row;
          align-items: center;
        }
        .checkbox-item {
          width: calc(100%);
          display: flex;
          flex-direction: row;
          align-items: center;
          position: relative;
          @media only screen and (max-width: 500px),
            only screen and (max-device-width: 500px) {
            flex-direction: column;
          }
          > span {
            overflow: hidden;
            white-space: pre-wrap;
          }
        }
      }
      .mine-button {
        width: 1.5rem;
        margin-top: 0.1rem;
        @media only screen and (max-width: 500px),
          only screen and (max-device-width: 500px) {
          width: auto;
        }
      }
    }
    .message {
      display: flex;
      flex-direction: column;
      .title {
        width: 100%;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        color: white;
        box-sizing: border-box;
        padding-left: 0.3rem;
        span {
          color: red;
        }
      }
      .content {
        color: white;
        margin-left: 0.3rem;
      }
      > span {
        font-size: @big-size;
        > i {
          margin-right: 0.05rem;
          color: #409eff;
        }
      }
      > p {
        color: @node-color;
        border-bottom: 1px @node-color solid;
        padding-bottom: 0.3rem;
      }
    }
    .lesson {
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      padding-right: 0;
      padding-bottom: 0;
      > div {
        width: calc((100% - 0.9rem) / 3);
        height: 2rem;
        margin-right: 0.3rem;
        margin-bottom: 0.3rem;
        position: relative;
        cursor: pointer;
        @media only screen and (max-width: 500px),
          only screen and (max-device-width: 500px) {
          width: 100%;
        }
        > img {
          width: 100%;
          height: 100%;
          position: absolute;
          object-fit: cover;
          left: 50%;
          // bottom: 0.5rem;
          transform: translateX(-50%);
          transition: 300ms;
        }
        > div {
          width: 100%;
          // height: 0.5rem;
          display: flex;
          justify-content: center;
          align-items: center;
          position: absolute;
          box-sizing: border-box;
          padding: 0.1rem;
          left: 50%;
          // top: 100%;
          bottom: 0;
          transform: translateX(-50%);
          background: rgb(45, 45, 45);
          transition: 300ms;
        }
      }
    }
    .set {
      display: flex;
      flex-direction: column;
      > span {
        font-size: @normal-size;
        color: @gold-color;
      }
      .el-input {
        width: 3rem;
        margin-bottom: 0.3rem;
        @media only screen and (max-width: 500px),
          only screen and (max-device-width: 500px) {
          width: 100%;
        }
      }
      .mine-button {
        width: 3rem;
        @media only screen and (max-width: 500px),
          only screen and (max-device-width: 500px) {
          width: 100%;
        }
      }
    }
    .recharge {
      flex-direction: column;
      align-items: center;
      > div {
        width: 100%;
        border-right: 1px @gold-color solid;
        border-bottom: 1px @gold-color solid;
        display: flex;
        flex-direction: column;
        > div {
          width: 100%;
          display: flex;
          flex-direction: row;
          > div {
            flex: 1;
            overflow: hidden;
            height: 0.4rem;
            border-left: 1px @gold-color solid;
            border-top: 1px @gold-color solid;
            display: flex;
            align-items: center;
            justify-content: center;
            text-align: center;
            font-size: @small-size;
          }
        }
      }
    }
  }
  .head {
    bottom: 0.7rem;
    > img {
      width: 1.4rem;
      height: 1.4rem;
      // background: @gold-color;
      border-radius: 50%;
      margin-left: 0.3rem;
      margin-right: 0.2rem;
      object-fit: cover;
    }
    > div {
      display: flex;
      flex-direction: column;
      > span {
        height: 0.7rem;
        display: flex;
        align-items: center;
        font-size: @title-size;
        > i {
          font-size: @title-size;
          transform: translateY(-50%);
          cursor: pointer;
          color: @node-color;
          transition: 300ms;
          margin-left: 0.1rem;
        }
        > i:hover {
          color: white;
        }
      }
      > div {
        display: flex;
        flex-direction: row;
        align-items: center;
        margin-top: 0.15rem;
        @media only screen and (max-width: 500px),
          only screen and (max-device-width: 500px) {
          flex-direction: column;
          align-items: flex-start;
        }
        .mine-button {
          width: 1.5rem;
          @media only screen and (max-width: 500px),
            only screen and (max-device-width: 500px) {
            width: auto;
          }
        }
        > span {
          margin-left: 0.3rem;
          color: @gold-color;
          @media only screen and (max-width: 500px),
            only screen and (max-device-width: 500px) {
            margin-left: 0;
            margin-top: 0.1rem;
          }
        }
      }
    }
  }
}
.avatar-uploader {
  margin-top: 0.2rem;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 0.05rem;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  width: 2rem;
  height: 2rem;
  font-size: @big-size;
  color: #8c939d;
  line-height: 2rem;
  text-align: center;
}
.avatar {
  width: 2rem;
  height: 2rem;
  border-radius: 0.05rem;
  object-fit: cover;
}
</style>
